<template>   

    <Form ref="formItem" :model="formItem" :label-width="80" :rules="ruleValidate">
        <FormItem label="学年学期">
            <Cascader v-model="years" :data="yearlist" :load-data="loadData"></Cascader>
        </FormItem>
        <FormItem label="类型" prop="type">
            <Select v-model="formItem.type">
                <Option value="1" key="1">上午</Option>
                <Option value="2" key="2">中午</Option>
                <Option value="3" key="3">下午</Option>
                <Option value="4" key="4">晚自习</Option>
            </Select>
        </FormItem>
        <FormItem label="开始时间" prop="time_an">
            <TimePicker v-model="time_an" type="time" @on-change="time_anfun" confirm placeholder="开始时间" ></TimePicker>
        </FormItem>
        <FormItem label="结束时间" prop="time_en">
            <TimePicker v-model="time_en" type="time" @on-change="time_enfun" confirm placeholder="结束时间" ></TimePicker>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="submit('formItem')">保存</Button>
        </FormItem>
        <div style="clear:both"></div>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>
export default {
    props: {
        infoId: {
            type: [Boolean,Number],
            default: false
        }
    },
    components: {
        SemesterChangeFuzhi:resolve => {require(['&/Authpub/SemesterChangeFuzhi.vue'], resolve);},
    },
    data () {
        return {
            time_an:null,//开始时间
            time_en:null,//结束时间
            years:[],   
            timeCenter:[],//判断学年学期的显示
            xspd:false,

            infoUrl:`api/teacher/v1/certificationdedey/find/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式

            spinShow:false,
            infoOld:[],//请求的数据

            formItem: {      
                time_an:'',
                time_en:'',
                type:null,//排序默认值
                attendance_management_id:'',
                attendance_type:'1'
            },
            yearlist:[],
            ruleValidate: {
                time_an: [
                    { required: true, type: 'string', message: '开始时间不得为空', trigger: 'change' }
                ],
                time_en: [
                    { required: true, type: 'string', message: '结束时间不得为空', trigger: 'change' }
                ],
                type: [
                    { required: true, message: '类型不得为空', trigger: 'change' }
                ],
                attendance_type: [
                    { required: true, message: '考勤类型不得为空', trigger: 'change' }
                ],
                // title: [
                //      { required: true, message: '标题不得为空', trigger: 'change' ,type:'string'}
                //  ],
            }
            
          
        }
    },
    methods: {
       
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        submit (name) {
             this.formItem.schoolyear_id=this.years[0]
            this.formItem.semester_id=this.years[1]
            if(this.formItem.semester_id!=null && this.formItem.schoolyear_id!=null){
            this.$refs[name].validate((valid) => {
                if (valid) {
                   
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formItem).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    }) 
                        
                } else {
                    this.$Message.error('验证失败');
                }
            })
            }
        },
        load() {     
            // 加载学年  
            this.$http.get('api/semester/v1/schoolyear/info').then((response) => { 
                this.yearlist=this.process_year(response.data)
                // this.Dis=false
            });  
        },
        // 学年处理数据
        process_year(data) {
            let arr = []
            data.forEach(item => {
                arr.push({
                    value : item.schoolyear_id,
                    label : item.schoolyear_name.zh,
                    children : [],
                    loading: false,
                    type: 'year'
                })
            })
            return arr
        },
        // 异步加载数据
        loadData(item, callback) {
            item.loading = true;
            // 点击的学年
            if(item.type == 'year'){
                this.$http.get('api/semester/v1/semester/all_semester?schoolyear_id='+item.value).then((response) => {
                    setTimeout(() => {
                        if(response.data.length!==0){
                            item.children=[]
                            response.data.forEach(item1 => {
                                item.children.push({
                                    value : item1.semester_id,
                                    label : item1.semester_name.zh,
                                    type:'year'
                                })
                            }) 
                        }else{ 
                            item.children.push({
                                value : '',
                                label : '',
                                type:'year'
                            })
                        }
                        item.loading = false;
                        callback();
                    }, 1000);
                });
            }
                
        },
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用
            this.$http.get(this.infoUrl).then(val=>{ 
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formItem.attendance_type=this.infoOld.attendance_type
                this.formItem.attendance_management_id=val.data.attendance_management_id
                this.time_an = this.formItem.time_an = this.infoOld.time_an//开始时间
                this.time_en = this.formItem.time_en = this.infoOld.time_en//结束时间
                this.formItem.type = this.infoOld.type.toString()//类型
                this.timeCenter=[this.infoOld.schoolyear_id,this.infoOld.semester_id]
                this.spinShow=false//输入框禁用 
                this.xspd=true 
            }).catch(err => {
                console.log(err);
            })
        },
        
        
        /************************************************************
         * 
         * 开始时间
         * 
         ************************************************************/
        time_anfun(obj){
            this.formItem.time_an=obj

        },
        
        /************************************************************
         * 
         * 结束时间
         * 
         ************************************************************/
        time_enfun(obj){
            this.formItem.time_en=obj

        },
        
        /************************************************************
         * 
         * 点击学年学期请求列表数据
         * 
         ************************************************************/
        peoplebutton(val){
            this.formItem.schoolyear_id=val[0]
            this.formItem.semester_id=val[1]
        },
        
    },
    mounted(){
        this.load();
        this.$http.get('api/semester/v1/semester/now_semester').then(val=>{
          this.years=[val.data[0].schoolyear_id,val.data[1].semester_id]
        })
        if(this.infoId==false){
            // add
            this.xspd=true
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/certificationdedey/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/certificationdedey/edit/${this.infoId}`
            this.saveType=this.$http.put
        }
    },
}
</script>
